/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Practica6.practica;

import java.io.File;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author miguel
 */
public class ArbolGenerador extends javax.swing.JFrame {

    /**
     * Creates new form ArbolGenerador
     */
    public ArbolGenerador() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonArchivo = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jComboBoxAlgoritmos = new javax.swing.JComboBox();
        jButtonEjecutar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButtonArchivo.setText("Seleccionar archivo");
        jButtonArchivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonArchivoActionPerformed(evt);
            }
        });

        jLabel1.setText("Archivo:");

        jComboBoxAlgoritmos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Algoritmo de Kruskal", "Algoritmo de Prim" }));

        jButtonEjecutar.setText("Ejecutar algoritmos");
        jButtonEjecutar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEjecutarActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonArchivo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBoxAlgoritmos, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonEjecutar)
                        .addGap(0, 290, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonArchivo)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxAlgoritmos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonEjecutar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonEjecutarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEjecutarActionPerformed
        // TODO add your handling code here:
        if (filename != null && !filename.equals("")) {
            g = new MyGraph(filename);
            jTextArea1.setText("");
            jTextArea1.append("Gráfica original\n\n");
            for (MyEdge e : (List<MyEdge>) g.getEdgesList()) {
                this.jTextArea1.append(e.toString() + "\n");
            }
            if (this.jComboBoxAlgoritmos.getSelectedItem().equals("Kruskal")) {
                jTextArea1.append("Árbol generador de peso mínimo con Kruskal\n");
                MyGraph g1 = (MyGraph) apm.Kruskal(g);
                for (MyEdge e : (List<MyEdge>) g.getEdgesList()) {
                    this.jTextArea1.append(e.toString() + "\n");
                }
            }else if(jComboBoxAlgoritmos.getSelectedItem().equals("Prim")){
                jTextArea1.append("Árbol generador de peso mínimo con Prim\n");
                MyGraph g1 = (MyGraph) apm.Prim(g,(MyVertex)g.getVertex(0));
                for (MyEdge e : (List<MyEdge>) g.getEdgesList()) {
                    this.jTextArea1.append(e.toString() + "\n");
                }
            }

        }else{
            JOptionPane.showMessageDialog(this, "Debes de seleccionar un archivo primero.");
        }
    }//GEN-LAST:event_jButtonEjecutarActionPerformed

    private void jButtonArchivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonArchivoActionPerformed
        // TODO add your handling code here:
        File f;
        f = new File(".");
        JFileChooser jfc = new JFileChooser();

        jfc.setCurrentDirectory(f);
        int returnVal = jfc.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            filename = jfc.getSelectedFile().getAbsolutePath();
            this.jLabel1.setText(filename);
        } else {
            JOptionPane.showMessageDialog(this, "Debes de escoger un archivo");
        }
    }//GEN-LAST:event_jButtonArchivoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ArbolGenerador.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ArbolGenerador().setVisible(true);
            }
        });
    }
    private ArbolDePesoMinimo apm = new ArbolDePesoMinimo();
    private String filename;
    private MyGraph g;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonArchivo;
    private javax.swing.JButton jButtonEjecutar;
    private javax.swing.JComboBox jComboBoxAlgoritmos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables
}
